/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matrimonybeans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

/**
 *
 * @author ChauAnh
 */
@Stateless
public class Request implements RequestLocal {

    ConnectDB conDB = new ConnectDB();
    Connection conn;

    @Override
    public int countRequest(String username) {
        try {
            conn = conDB.getConnection();
            String strSql = "SELECT COUNT(*) AS receiverCount FROM SendRequest WHERE receiver=? AND status='False'";
            PreparedStatement pstmt = conn.prepareCall(strSql);
            pstmt.setString(1, username);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt("receiverCount");
                return count;
            }
            conn.close();

        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    @Override
    public Boolean sendRequest(String username, String receiver, String date, String status) {
        boolean flag = false;
        try {
            conn = conDB.getConnection();
            String strSql = "INSERT INTO SendRequest VALUES(?,?,?,?)";
            PreparedStatement pstmt = conn.prepareCall(strSql);
            pstmt.setString(1, username);
            pstmt.setString(2, receiver);
            pstmt.setString(3, date);
            pstmt.setString(4, status);
            pstmt.execute();
            flag = true;                       

        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        return flag;
    }

    @Override
    public ResultSet viewAll(String receiver) {
        try {
            conn = conDB.getConnection();
            String strSql = "SELECT SendRequest.id_send_request, SendRequest.username, SendRequest.receiver, SendRequest.date, SendRequest.status, Candidate.first_name, Candidate.middle_name, Candidate.last_name, Candidate.avatar"                    
                    + " FROM SendRequest INNER JOIN Candidate ON SendRequest.username = Candidate.username"                    
                    + " WHERE receiver = ? ";
            PreparedStatement pstmt = conn.prepareCall(strSql);
            pstmt.setString(1, receiver);
            ResultSet rs = pstmt.executeQuery();
            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        return null;
    }
    
    

    @Override
    public Boolean deleteSendRequest(int id_send_request) {
        boolean flag = false;
        try {
            conn = conDB.getConnection();
            Statement stm = conn.createStatement();
            String strSql = "DELETE SendRequest WHERE id_send_request = '"+id_send_request+"'";
            stm.executeUpdate(strSql);
            flag = true;
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        return flag;
    }

    @Override
    public HashMap getAllRequest(String receiver) {
        HashMap requestData = new HashMap();
        try {
            conn = conDB.getConnection();
            String strSql = "SELECT SendRequest.id_send_request, SendRequest.username, SendRequest.receiver, SendRequest.date, SendRequest.status, Candidate.first_name, Candidate.middle_name, Candidate.last_name, Candidate.avatar"                    
                    + " FROM SendRequest INNER JOIN Candidate ON SendRequest.username = Candidate.username"                    
                    + " WHERE SendRequest.receiver = ? "
                    + " ORDER BY SendRequest.id_send_request DESC";
            PreparedStatement pstmt = conn.prepareCall(strSql);
            pstmt.setString(1, receiver);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                HashMap hm = new HashMap();
                hm.put("id_send_request", rs.getString("id_send_request").trim());
                hm.put("username", rs.getString("username").trim());
                hm.put("receiver", rs.getString("receiver").trim());
                hm.put("date", rs.getString("date").trim());
                hm.put("status", rs.getString("status").trim());
                hm.put("first_name", rs.getString("first_name").trim());
                hm.put("middle_name", rs.getString("middle_name").trim());
                hm.put("last_name", rs.getString("last_name").trim());
                hm.put("avatar", rs.getString("avatar").trim());
                requestData.put(rs.getString("id_send_request").trim(), hm);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        
        return requestData;
    }
    
    
}
